/**
 * Created by c.k on 2017/11/10.
 * 从url 地址中获取渠道号
 */
import { querystring } from 'vux'

export default {
    // parse channel
    getCnVal() {  // 返回渠道号
        let _cnval = '';
        let _clientInfo = window.localStorage.getItem('newClientInfo');
        _clientInfo = JSON.parse(_clientInfo);
        // 如果有client信息
        if (_clientInfo && _clientInfo.cn) {
            if (this.GetUrlParam('cn')) {
                _clientInfo.cn = this.GetUrlParam('cn');
                window.localStorage.setItem('newClientInfo', JSON.stringify(_clientInfo));
            }
            return _clientInfo.cn;
        } else {
            // 第一次h5没有设备信息的话，就从地址栏中获取cn
            _clientInfo = {};
            _cnval = this.GetUrlParam('cn');
            _clientInfo.cn = _cnval;
            window.localStorage.setItem('newClientInfo', JSON.stringify(_clientInfo));
            return _cnval;
        }
    },
    GetUrlParam(paraName) {  // 从地址栏中获取渠道号
        let _url = window.location.href;
        let index = _url.indexOf('?');
        if (index !== -1) {
            let queryParams = _url.substr(index+1);
            if (querystring.parse(queryParams)[paraName]) {
                return querystring.parse(queryParams)[paraName]
            }
        }
        return '';
    }
};
